<template>
  <div class="login-container">
    <el-row class="row-container">
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login-form" :model="loginForm" :rules="rules" ref="loginForms">
          <el-form-item>
            <h1>欢迎来到后台管理系统</h1>
          </el-form-item>
          <el-form-item prop="username">
            <el-input type="text" v-model="loginForm.username" :prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" :prefix-icon="Lock" show-password></el-input>
          </el-form-item>
          <el-form-item class="login-button-item">
            <el-button :loading="loading" class="login-button" type="primary" @click="login()">登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import {User, Lock} from "@element-plus/icons-vue";
import {reactive, ref, useTemplateRef} from "vue";
import useUserStore from "@/store/modules/user.ts";
import {useRoute, useRouter} from "vue-router";
import {ElNotification, type FormInstance} from "element-plus";
import {getTime} from "@/utils/time.ts";

let userStore = useUserStore();
let $router = useRouter()
let $route = useRoute()

let loginForm = reactive({
  username: 'admin',
  email: '2632686733@qq.com',
  password: '111111'
})

let rules = reactive({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    // {pattern: /^[a-zA-Z0-9_-]{4,16}$/, message: '用户名应为4-16位字母数字组合'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 16, message: '密码长度在6-16位', trigger: 'blur'}
  ]
})
// 表单ref引用
let loginFormRef = useTemplateRef<FormInstance>('loginForms')

let loading = ref(false)


// 登录按钮回调
const login = async () => {
  try {
    await loginFormRef.value?.validate()
    loading.value = true
    await userStore.userLoginStore(loginForm)
    let redirect = $route.query.redirect as string
    await $router.replace({path: redirect || '/home'})
    ElNotification({
      type: 'success',
      title: `HI, ${getTime()}好`,
      message: '欢迎回来'
    })
    loading.value = false
  } catch (e: any) {
    if (e.password) return
    ElNotification({
      type: 'error',
      title: '登录失败',
      message: e
    })
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .row-container {
    position: relative;
    top: 40vh;
  }

  .login-form {
    width: 80%;
    padding: 50px 40px;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;

    h1 {
      color: white;
      font-size: 30px;
    }

    .login-button-item {
      margin-bottom: 0;

      .login-button {
        width: 100%;
      }
    }
  }
}
</style>
